package udp2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

public class UDPSendDemo {
	public static void main(String[] args) throws IOException {
		System.out.println("发送端启动......");
		// 1.udpsocket服务,使用DatagramSocket对象
		DatagramSocket ds = new DatagramSocket();

		// 2.将要发送的数据封装到数据包中
		// String string = "udp传输演示,哥们来了！";
		BufferedReader bufferedReader = new BufferedReader(
				new InputStreamReader(System.in));
		String line = null;
		while ((line = bufferedReader.readLine()) != null) {
			if ("over".equals(line)) {
				break;
			}
			byte[] buf = line.getBytes();
			DatagramPacket dp = new DatagramPacket(buf, buf.length,
					InetAddress.getByName("127.0.0.1"), 8081);
			ds.send(dp);
		}
		ds.close();
	}
}
